package com.blackline.auto.Report;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * 			Testing environment: T2
 * 			Instance: Test 2
 * 			Test Rail ID :- C44862
 */


public class DeleteUPSReports { 

	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_smoketest.class);
	UI UI=new UI();
	int sleepTime= 2000;
	Parameters parameters;
	int i=0;
	String NameofReport;
	
	TestData testdata = new TestData("TestData/ModifyDate_ScheduledReports.xml");

	String startDate = testdata.getInput("StartDateForDelete");
	String endDate = testdata.getInput("EndDateForDelete"); 

	
	@Before
	public void setUp() throws Exception {
		
		
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);	
	}

	@After
	public void tearDown() throws Exception
	{
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}

	/**
	 * This is to all reports from Report Log
	 * Filters 'Failed' Reports and on the basis of 'Start Date' and 'End Date'
	 * 
	 */
	
	
	@Test
	public void testDeleteFailedReports() throws Exception
	{
		logger.info("You've selected site: https://t2.blacklineondemand.com/Test3");
		UI.LogonPage.Login(driver, "https://t2.blacklineondemand.com/Test2", parameters.getLogonID("admin"), parameters.getPassword("admin"));
		Thread.sleep(sleepTime);
		
		//Go to Reports link
		UI.NavigationMenu.REPORTS.click();
		UI.Reports.Enhanced_Reporting_link.click();
		Thread.sleep(sleepTime+5000);
		
		UI.Reporting.Status_text_field.clear();
		UI.Reporting.Status_text_field.sendKeys("Failed");
		UI.Reporting.StartDate_text_field.clear();
		UI.Reporting.StartDate_text_field.sendKeys(startDate);
		UI.Reporting.EndDate_text_field.clear();
		UI.Reporting.EndDate_text_field.sendKeys(endDate);

		UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
		UI.Reporting.StartDate_text_field.sendKeys(Keys.RETURN);
		UI.Reporting.EndDate_text_field.sendKeys(Keys.RETURN);

		
		Thread.sleep(sleepTime+5000);
		
		String Results= UI.Reporting.Number_of_results.getText();
		String delims = " ";
		String[] tokens = Results.split(delims);
		int number_of_results=Integer.parseInt(tokens[3]);
		
		Thread.sleep(sleepTime);
		
		if(number_of_results==0)
		{
			logger.info("No reports found");
			return;
		}
		
		Thread.sleep(sleepTime);
		logger.info("Number of Failed Reports (before Deletion): "+ number_of_results);
		
		Thread.sleep(sleepTime); 

		for(int i =0; i<number_of_results; i++)
		{
			UI.Reporting.Delete_linkOfSearchedResult.click();
			Thread.sleep(sleepTime+2000);

		}
		
		Thread.sleep(sleepTime+5000);
		Results= UI.Reporting.Number_of_results.getText();
		tokens = Results.split(delims);
		number_of_results=Integer.parseInt(tokens[3]);
		logger.info("Number of Failed Reports (after Deletion): "+ number_of_results);
		Thread.sleep(sleepTime); 
		
		if(number_of_results==0)
		{
			logger.info("All Failed Reports deleted.");
		}

	}
	
}
